package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;





import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import model.AppModel;
import model.ModelException;
import model.Filter;


import view.FiltersView;

import view.ViewHelpers;

/**
 * Controls the Filters view
 * 
 * This controller has a reference to the main model, and the current view it is working on.
 * It assigns handlers to events for the edit, add, delete, apply and selection change
 * buttons and specifies the actions to invoke upon triggering these events. 
 * 
 * @author MUSAB
 *
 * @version $Revision: 1.0 $
 */
public class FiltersViewController {
	
	// Application model, view and main controller
	private AppModel model;
	
	// The view to work on
	private FiltersView view;
	

	/**
	 * Constructor takes the app model
	 * @param model
	 */
	public FiltersViewController(AppModel model) {
		
		this.model = model; 
		view = new FiltersView();
		
		refreshView();
		assignHandlersToEvents();
		view.showDialog();
	}
	
	/**
	 * Load filters into view
	 */
	private void refreshView(){
		
		view.getFilters().removeAllElements();
		for (Filter filter : model.getUser().getFilters()){
			view.getFilters().addElement(filter.getTitle());
		}
	}

	
	/**
	 * Adds handlers to events for the filter view
	 */
	private void assignHandlersToEvents() {
		
		/**
		 * Selection changed for jlist
		 */
		view.getFiltersList().addListSelectionListener(new ListSelectionListener() {
			
			@Override
			public void valueChanged(ListSelectionEvent e) {
				actionShowFilterPreview();
			}
		});
	
		
		
		/**
		 * Add new filter button click
		 */
		view.getAddNewBtn().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				actionCreateNewFilter();
			}
		});
		
		/**
		 * Edit filter button click
		 */
		view.getEditbtn().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				actionEditFilter();
			}
		});
		
		/**
		 * Apply filter button
		 */
		view.getApplyBtn().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				actionApplyFilter();
			}
		});
		
		/**
		 * delete filter button click
		 */
		view.getDeleteBtn().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				actionDeleteFilter();
			}
		});
		
	}

	/**
	 * Applies the currently selected filter
	 */
	protected void actionApplyFilter() {
		
		String filterTitle = view.getFiltersList().getSelectedValue();
		if (filterTitle == null){
			ViewHelpers.alertError("Please select a filter first");
			return;
		}
		
		Filter filter = model.getUser().getFilter(filterTitle);
		
		if (filter == null){
			ViewHelpers.alertError("The selected filter does not exist any more, might have been deleted");
			refreshView();
			return;
		}
		
		model.applyFilter(filter);
	}

	/**
	 * Shows the filter preview of the filter selected in the list
	 */
	protected void actionShowFilterPreview() {
		
		String filterTitle = view.getFiltersList().getSelectedValue();
		
		// If none selected, clear its
		if (filterTitle == null){
			view.getBody().setText("");
			return;
		}
		
		// Show selected filter
		Filter filter = model.getUser().getFilter(filterTitle);
		String folderName = model.getUser().getFolder(filter.getFolderId()).getName();
		String previewText = "FILTER TITLE: " + filter.getTitle() +
				"\n----------------------------" +
				"\n" +
				"\nCriteria:-" +
				"\n\nSubject: " + filter.getSubject() +
				"\n\nTo: " + filter.getTo() +
				"\n\nFrom: " + filter.getFrom() +
				"\n\nBody: " + filter.getBody() +
				"\n\n\nPut into folder: " + folderName;
		view.getBody().setText(previewText);
	}

	/**
	 * Confirms for filter deletion and then deletes that filter
	 */
	protected void actionDeleteFilter() {
		
		String filterTitle = view.getFiltersList().getSelectedValue();
		if (filterTitle == null){
			ViewHelpers.alertError("Please select a filter first");
			return;
		}
		
		if (ViewHelpers.confirm("Are you sure you want to delete this filter?")){
			try {
				int id = model.getUser().getFilter(filterTitle).getId();
				model.getUser().deleteFilter(id);
				refreshView();
			} catch (ModelException e) {
				ViewHelpers.alertError("Could not delete the filter, please try again later");
				e.printStackTrace();
			}
		}
	}

	/**
	 * Shows the popup to edit an existing filter
	 */
	protected void actionEditFilter() {
		
		String filterTitle = view.getFiltersList().getSelectedValue();
		if (filterTitle == null){
			ViewHelpers.alertError("Please select a filter first");
			return;
		}
		
		Filter t = null;
		for (Filter filter : model.getUser().getFilters()){
			if (filter.getTitle().trim().equalsIgnoreCase(filterTitle)){
				t = filter;
				break;
			}
		}
		new EditFilterController(model, t);
		refreshView();
	}

	/**
	 * Shows the popup to add new filter
	 */
	protected void actionCreateNewFilter() {

		new EditFilterController(model, null);
		refreshView();
	}
}
